// shots fired by my ship, or from enemies

function Shot(AFromX,AFromY,AToX,AToY,ASpreadAngle) {
  // create single shot
  this.x = AFromX;
  this.y = AFromY;
  this.speed = 12;
  this.size = ship.penetration;
  this.angle = Math.atan2(AToY-AFromY,AToX-AFromX)+ASpreadAngle*Math.PI/180;
  this.dx = this.speed*Math.cos(this.angle); //-ship.dx;
  this.dy = this.speed*Math.sin(this.angle); //-ship.dy;
  this.range = ship.range/this.speed;
  // drawing procedure
  this.draw = function() {
    // moving shot forward
    this.x = this.x + this.dx;
    this.y = this.y + this.dy;
    this.range--;
    // draw shot
    context.save();
    context.translate(-x0+this.x-ship.x,-y0+this.y-ship.y);
    // improve appearance of used shots
    if (this.size <= 0) { 
      this.size -= 3;
      this.speed *= 0.8;
    }
    // draw it
    if (this.size > 0)
      context.fillStyle = "rgba(255, 255, 0, 1.0)";
    else
      context.fillStyle = "rgba(255, 0, 0, "+(1/Math.abs(this.size/3))+")";
    if (this.size != 0) {
      var as = Math.abs(this.size);      
      context.fillRect(-as,-as,2*as,2*as); // NOTE: for some reson, opera crash if fillRect is given +/- weird coords, e.g. negative size or so
    } 
    context.restore();
  } 
}

function Shots() {
  // all shots
  this.shots = new Array();

  // adding function
  this.push = function(AShot) {
    this.shots.push(AShot);
  }
   
  // drawing function
  this.draw = function() {
    for (var i=this.shots.length-1; i>=0; i--) {
      this.shots[i].draw();
      // remove unused
      if (this.shots[i].range <= 0)
        this.shots.splice(i,1);
    }
  }
  
  // debug function
  this.debug = function() {
    var s = 'shots: '+this.shots.length+'\n';
    for (var i=0; i<this.shots.length; i++)
      s += 'shots['+i+']: x='+this.shots[i].x+' y='+this.shots[i].y+'\n';
    return s+'\n';      
  }
  
}
